#include "msginf.h"
#include <regex>

QString MSGINF::MsgGetItem(QWidget *parent,QString title,QString label,QStringList items,bool *ok)
{
    QInputDialog *dlg = new QInputDialog(parent);
    //dlg->setWindowFlags(Qt::Dialog|Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    dlg->setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    dlg->setWindowTitle(title);
    dlg->setLabelText(label);
    dlg->setOkButtonText(_("Ok"));
    dlg->setCancelButtonText(_("Cancel"));
    dlg->setComboBoxEditable(false);
    dlg->setComboBoxItems(items);
    *ok = dlg->exec();

    return dlg->textValue();


}

int MSGINF::MsgShowOk(QWidget *parent,QString title,QString label,QMessageBox::StandardButtons bOk)
{
    QMessageBox box(QMessageBox::Information,title,label,bOk,parent);
    //box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
    box.setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    box.setStandardButtons (bOk);
    box.setButtonText (QMessageBox::Ok,QString(_("Ok")));
    box.setButtonText (QMessageBox::Cancel,QString(_("Cancel")));
    return box.exec();

}
int MSGINF::MsgAbout(QWidget *parent,QString title,QString label,QMessageBox::StandardButtons bOk)
{
    QMessageBox box(QMessageBox::Information,title,label,bOk,parent);
    QPixmap bmp = QPixmap(":/fsapp.ico");
    QPixmap pix = bmp.scaled(64,64);
    box.setIconPixmap(pix);
    //box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
    box.setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    box.setStandardButtons (bOk);
    box.setButtonText (QMessageBox::Ok,QString(_("Ok")));
    box.setButtonText (QMessageBox::Cancel,QString(_("Cancel")));
    return box.exec();

}
QString MSGINF::MsgGetText(QWidget *parent,QString title,QString label,bool *ok)
{
    QInputDialog *dlg = new QInputDialog(parent);
    dlg->setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    dlg->setWindowTitle(title);
    dlg->setLabelText(label);
    dlg->setTextEchoMode(QLineEdit::Normal);
    dlg->setInputMode(QInputDialog::TextInput);
    dlg->setOkButtonText(_("Ok"));
    dlg->setCancelButtonText(_("Cancel"));

    *ok = dlg->exec();

    return dlg->textValue();
}

std::vector<std::string> MSGINF::stringSplit(const std::string& str, char delim)
{
    std::string s;
    s.append(1, delim);
    std::regex reg(s);
    std::vector<std::string> elems(std::sregex_token_iterator(str.begin(), str.end(), reg, -1),
                                   std::sregex_token_iterator());
    return elems;
}
string& MSGINF::replace_str(string& str, const string& to_replaced, const string& newchars)
{
    if(to_replaced.length()<=0) return str;
    for (string::size_type pos(0); pos != string::npos; pos += newchars.length())
    {
        pos = str.find(to_replaced, pos);
        if (pos!=string::npos)
            str.replace(pos, to_replaced.length(), newchars);
        else
            break;
    }
    return   str;
}
